Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports Cforge.Security.Certificates

Namespace ImapClientDemo.Security
	''' <summary>
	''' Form used for showing certificate's issues and let user to decide Accept or Reject the certificate.
	''' </summary>
	Partial Public Class CertValidator
		Inherits Form
		Private _showAddToTrustedList As Boolean
		''' <summary>
		''' Gets or sets a boolean value indicating whether the button "Add To Trusted List" will be shown.
		''' </summary>
		Public Property ShowAddToTrustedList() As Boolean
			Get
				Return _showAddToTrustedList
			End Get
			Set(ByVal value As Boolean)
				_showAddToTrustedList = value
			End Set
		End Property

		Private _addToTrustedList As Boolean
		''' <summary>
		''' Gets a boolean value indicating whether the certificate will be added to the local PC's trusted list.
		''' </summary>
		Public ReadOnly Property AddToTrustedList() As Boolean
			Get
				Return _addToTrustedList
			End Get
		End Property

		Private _accepted As Boolean
		''' <summary>
		''' Gets a boolean value indicating whether the certificate is accepted.
		''' </summary>
		Public ReadOnly Property Accepted() As Boolean
			Get
				Return _accepted
			End Get
		End Property

		Private _cert As X509Certificate
		''' <summary>
		''' Gets or sets the certificate object.
		''' </summary>
		Public Property Certificate() As X509Certificate
			Get
				Return _cert
			End Get
			Set(ByVal value As X509Certificate)
				_cert = value
			End Set
		End Property

		Private _issues As String
		''' <summary>
		''' Gets or sets the certificate's issues.
		''' </summary>
		Public Property Issues() As String
			Get
				Return _issues
			End Get
			Set(ByVal value As String)
				_issues = value
			End Set
		End Property

		''' <summary>
		''' Handles the Trusted button's Click event.
		''' </summary>
		''' <param name="sender">The button object.</param>
		''' <param name="e">The event arguments.</param>
		Private Sub btnTrusted_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTrusted.Click
			_addToTrustedList = True
			_accepted = True
			Close()
		End Sub

		''' <summary>
		''' Handles the Cancel button's Click event.
		''' </summary>
		''' <param name="sender">The button object.</param>
		''' <param name="e">The event arguments.</param>
		Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
			' User rejected this certificate and close this form.
			Close()
		End Sub

		''' <summary>
		''' Handles the OK button's Click event.
		''' </summary>
		''' <param name="sender">The button object.</param>
		''' <param name="e">The event arguments.</param>
		Private Sub btnOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOk.Click
			' User accepted this certificate and close this form.
			_accepted = True
			Close()
		End Sub

		''' <summary>
		''' Initializes a new instance of the CertValidator class.
		''' </summary>
		Public Sub New()
			InitializeComponent()
		End Sub

		''' <summary>
		''' Handles the form's Load event.
		''' </summary>
		''' <param name="e">The arguments for the event.</param>
		Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
			MyBase.OnLoad(e)

			' Update label controls.
			lblIssuerText.Text = _cert.Issuer.OrganizationName
			lblUnitText.Text = _cert.Issuer.UnitName
			lblLocationText.Text = String.Format("Country Code: {0}, State: {1}, City: {2}", _cert.Issuer.CountryCode, _cert.Issuer.State, _cert.Issuer.City)
			lblCommonNameText.Text = _cert.Issuer.CommonName

			lblOrganizationSubText.Text = _cert.Subject.OrganizationName
			lblUnitSubText.Text = _cert.Subject.UnitName
			lblLocationSubText.Text = String.Format("Country Code: {0}, State: {1}, City: {2}", _cert.Subject.CountryCode, _cert.Subject.State, _cert.Subject.City)
			lblCommonNameSubText.Text = _cert.Subject.CommonName

			lblFromText.Text = _cert.EffectiveDate.ToString()
			lblToText.Text = _cert.ExpiryDate.ToString()

			txtIssues.Text = _issues
		End Sub
	End Class
End Namespace